package com.teatang.tadmin.sysmenu.service;

import com.teatang.tadmin.core.page.BaseReturn;
import com.teatang.tadmin.core.page.PageDataReturn;
import com.teatang.tadmin.sysmenu.mapper.SysMenuMapper;
import com.teatang.tadmin.sysmenu.pojo.SysMenu;
import com.teatang.tadmin.sysmenu.vo.SysMenuIndexVo;
import com.teatang.tadmin.util.PageUtils;
import com.teatang.tadmin.util.TreeUtils;
import com.teatang.tadmin.util.text.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysMenuServiceImpl implements SysMenuService {
    @Autowired
    private SysMenuMapper menuMapper;

    @Override
    public Map<String, Object> menuInfo() {
        Map<String, Object> map = new HashMap<>(16);
        List<SysMenu> menuList = menuMapper.getMenuList("*");
        List<SysMenuIndexVo> menuInfo = new ArrayList<>();
        for (SysMenu e : menuList) {
            SysMenuIndexVo menuVO = new SysMenuIndexVo();
            menuVO.setId(e.getId());
            menuVO.setPid(e.getPid());
            menuVO.setHref(e.getHref());
            menuVO.setTitle(e.getTitle());
            menuVO.setIcon(e.getIcon());
            menuInfo.add(menuVO);
        }
        map.put("menuInfo", TreeUtils.toTree(menuInfo, 0L));

        Map<String, Object> home = new HashMap<>(16);
        home.put("title", "首页");
        home.put("href", "index-page/welcome-1.html");//控制器路由,自行定义

        Map<String, Object> logo = new HashMap<>(16);
        logo.put("title", "T Admin");
        logo.put("image", "images/logo.png");//静态资源文件路径,可使用默认的logo.png

        map.put("homeInfo", home);
        map.put("logoInfo", logo);

        return map;
    }

    @Override
    public PageDataReturn menuList() {
        List<SysMenu> menuList = menuMapper.getMenuList("*");
        return PageUtils.getDataPage(menuList, menuList.size());
    }

    @Override
    public PageDataReturn menuTree(Long pid) {
        List<SysMenu> menuList = menuMapper.getMenuList("id,pid,title");
        List<SysMenu> menuTree = TreeUtils.toTree(menuList, pid);
        return PageUtils.getDataPage(menuTree, menuTree.size());
    }

    @Override
    public SysMenu getSysMenuById(int id) {
        return menuMapper.getMenuById(id);
    }

    @Override
    public BaseReturn insertSysMenu(SysMenu sysMenu) {
        int status = menuMapper.insertSysMenu(sysMenu);
        return PageUtils.success(String.valueOf(status));
    }

    @Override
    public BaseReturn updateSysMenu(SysMenu sysMenu) {
        int status = menuMapper.updateSysMenu(sysMenu);
        return PageUtils.success(String.valueOf(status));
    }

    @Override
    public BaseReturn deleteSysMenuByIds(String ids) {
        Long[] userIds = Convert.toLongArray(ids);
        int flag = menuMapper.deleteSysMenuByIds(userIds);
        return PageUtils.success(String.valueOf(flag));
    }
}
